package com.cskaoyan.service.vx;

import com.cskaoyan.bean.bo.vxBo.WxGoodListBO;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.bean.vo.WxGoodsProductVO;
import com.cskaoyan.bean.vo.vxVo.*;
import com.cskaoyan.mapper.wxMapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Autowired
    MarketGrouponMapper marketGrouponMapper;

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public Integer goodsCount() {
        return marketGoodsMapper.selectGoodsCount();
    }

    @Override
    public WxGoodsCategoryVO goodsCategory(Integer id) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        List<MarketCategory> BMarketCategories = null;
        MarketCategory PMarketCategory = null;
        if (marketCategory.getPid() == 0) {
            marketCategoryExample.createCriteria().andPidEqualTo(marketCategory.getId());
            BMarketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
            PMarketCategory = marketCategory;
            marketCategory = BMarketCategories.get(0);
        } else {
            marketCategoryExample.createCriteria().andPidEqualTo(marketCategory.getPid());
            PMarketCategory = marketCategoryMapper.selectByPrimaryKey(marketCategory.getPid());
            BMarketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        }
        WxGoodsCategoryVO wxGoodsCategoryVO = new WxGoodsCategoryVO();
        wxGoodsCategoryVO.setCurrentCategory(marketCategory);
        wxGoodsCategoryVO.setBrotherCategory(BMarketCategories);
        wxGoodsCategoryVO.setParentCategory(PMarketCategory);
        return wxGoodsCategoryVO;
    }

    @Override
    public WxGoodListVO goodsList(WxGoodListBO wxGoodListBO) {
        WxGoodListVO wxGoodListVO = new WxGoodListVO();
        PageHelper.startPage(wxGoodListBO.getPage(), 40);
        List<WxIndexGoodsVO> wxIndexGoodsVOList = marketGoodsMapper.selectGoodsByCategoryId(wxGoodListBO.getCategoryId(), wxGoodListBO);
        List<MarketCategory> marketCategoryList = marketCategoryMapper.selectCategoryPidNotZero();
        PageInfo pageInfo = new PageInfo(wxIndexGoodsVOList);
        long total = pageInfo.getTotal();
        wxGoodListVO.setTotal((int) total);
        wxGoodListVO.setList(wxIndexGoodsVOList);
        wxGoodListVO.setFilterCategoryList(marketCategoryList);
        wxGoodListVO.setLimit(wxGoodListBO.getLimit());
        wxGoodListVO.setPages(pageInfo.getPages());
        wxGoodListVO.setPage(wxGoodListBO.getPage());
        return wxGoodListVO;
    }

    @Override
    public WxGoodsDetailVO goodDetail(Integer id, MarketUser marketUser) {
        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();
        List<String> names = marketGoodsSpecificationMapper.selectSpecNameByGoodsId(id);
        List<WxSpecificationVO> specificationVOList = new ArrayList<>();
        if (names != null) {
            for (String name : names) {
                WxSpecificationVO wxSpecificationVO = new WxSpecificationVO();
                wxSpecificationVO.setName(name);
                MarketGoodsSpecificationExample Sexample = new MarketGoodsSpecificationExample();
                Sexample.createCriteria().andSpecificationEqualTo(name).andDeletedEqualTo(false).andGoodsIdEqualTo(id);
                ;
                List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(Sexample);
                wxSpecificationVO.setValueList(marketGoodsSpecifications);
                specificationVOList.add(wxSpecificationVO);
            }
        }
        wxGoodsDetailVO.setSpecificationList(specificationVOList);

        List<MarketGroupon> marketGroupons = new ArrayList<>();
        if (marketUser.getId() == null) {
            wxGoodsDetailVO.setGroupon(marketGroupons);
        } else {
            MarketGrouponExample grouponExample = new MarketGrouponExample();
            grouponExample.createCriteria().andUserIdEqualTo(marketUser.getId()).andDeletedEqualTo(false);
            marketGroupons = marketGrouponMapper.selectByExample(grouponExample);
            wxGoodsDetailVO.setGroupon(marketGroupons);
        }


        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssueExample.createCriteria().andDeletedEqualTo(false);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        wxGoodsDetailVO.setIssue(marketIssues);

        if (marketUser.getId() == null) {
            wxGoodsDetailVO.setUserHasCollect(0);
        } else {
            MarketCollectExample marketCollectExample = new MarketCollectExample();
            marketCollectExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(marketUser.getId()).andTypeEqualTo((byte) 0).andValueIdEqualTo(id);
            int l = (int) marketCollectMapper.countByExample(marketCollectExample);
            wxGoodsDetailVO.setUserHasCollect(l);
        }


        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        wxGoodsDetailVO.setShareImage(marketGoods.getShareUrl());

        WxGoodsCommentVO wxGoodsCommentVO = new WxGoodsCommentVO();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.createCriteria().andValueIdEqualTo(id).andDeletedEqualTo(false);
        int count = (int) marketCommentMapper.countByExample(marketCommentExample);
        wxGoodsCommentVO.setCount(count);

        List<MarketComment> marketComments = marketCommentMapper.selectByValueIdAndUserId(id);
        List<WxGoodsCommentDataVO> wxGoodsCommentDataVOList = new ArrayList<>();
        if (marketComments != null) {
            for (MarketComment marketComment : marketComments) {
                MarketUser marketUser1 = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());
                if (marketUser1 != null  && marketUser1.getId() != null ) {
                    WxGoodsCommentDataVO wxGoodsCommentDataVO = WxGoodsCommentDataVO.getWxGoodsCommentDataVO(marketComment, marketUser1);
                    wxGoodsCommentDataVOList.add(wxGoodsCommentDataVO);
                }
            }
        }
        wxGoodsCommentVO.setData(wxGoodsCommentDataVOList);
        wxGoodsDetailVO.setComment(wxGoodsCommentVO);

        wxGoodsDetailVO.setShare(true);

        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        marketGoodsAttributeExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        wxGoodsDetailVO.setAttribute(marketGoodsAttributes);

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(marketGoods.getBrandId());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        MarketBrand marketBrand = new MarketBrand();
        if (marketBrands.size() > 0) {
            marketBrand = marketBrands.get(0);
            wxGoodsDetailVO.setBrand(marketBrand);
        }

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        marketGoodsProductExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        List<WxGoodsProductVO> wxGoodsProductVOList = new ArrayList<>();
        for (MarketGoodsProduct marketGoodsProduct : marketGoodsProducts) {
            wxGoodsProductVOList.add(WxGoodsProductVO.getWxGoodsProductVO(marketGoodsProduct));
        }
        wxGoodsDetailVO.setProductList(wxGoodsProductVOList);


        WxGoodsInfoVO wxGoodsInfoVO = WxGoodsInfoVO.getWxGoodsInfoVO(marketGoods);
        wxGoodsDetailVO.setInfo(wxGoodsInfoVO);

        return wxGoodsDetailVO;
    }

    @Override
    public TVO<WxIndexGoodsVO> goodRelated(Integer id) {
        //找出类别id
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer categoryId = marketGoods.getCategoryId();

        //类别id找同类goods
        List<WxIndexGoodsVO> goodsVOListTemp = marketGoodsMapper.selectGoodsInfoByCategoryId(id, marketGoods);

        List<WxIndexGoodsVO> goodsVOList = new ArrayList<>(goodsVOListTemp);
        TVO<WxIndexGoodsVO> wxIndexGoodsVOTVO = new TVO<>();
        if (goodsVOList.size() < 6) {
            int size = 6 - goodsVOList.size();
            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(categoryId);
            List<Integer> categoryIdList = marketCategoryMapper.selectCategoryIdByPid(marketCategory.getPid());
            PageHelper.startPage(1, size);
            List<WxIndexGoodsVO> goods = marketGoodsMapper.selectGoodsByCategoryIdAndSize(categoryIdList);
            PageInfo pageInfo = new PageInfo(goods);
            long total = pageInfo.getTotal();
            wxIndexGoodsVOTVO.setTotal((int) total);
            goodsVOList.addAll(goods);
            wxIndexGoodsVOTVO.setPages(goodsVOList.size() / 6 + 1);
        } else {
            wxIndexGoodsVOTVO.setTotal(goodsVOList.size());
            wxIndexGoodsVOTVO.setPages(goodsVOList.size() / 6 + 1);
            goodsVOList = goodsVOList.subList(0, 6);
        }

        wxIndexGoodsVOTVO.setLimit(6);
        wxIndexGoodsVOTVO.setPage(1);
        wxIndexGoodsVOTVO.setList(goodsVOList);
        return wxIndexGoodsVOTVO;
    }

    @Override
    public void insertFoot(Integer goodsId, Integer userId) {
        if (userId == null) {
            return;
        }
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setDeleted(false);
        marketFootprint.setAddTime(new Date());
        marketFootprint.setUpdateTime(new Date());
        marketFootprint.setGoodsId(goodsId);
        marketFootprint.setUserId(userId);
        int insert = marketFootprintMapper.insert(marketFootprint);
    }
}
